package com.ylcy.ads.schedule;

import com.ylcy.ads.config.ENV;
import com.ylcy.ads.utils.Utils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

/**
 * 刷新班次信息
 */
public class LoopService {
    public static Thread thread = null;
    public static boolean loop = true;
    /**
     * 启动调度
     */
    public static void start() {
        loop = true;
        if(thread == null){
            thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        if(loop){
                            HttpURLConnection connection = null;
                            BufferedReader reader = null;
                            try {
                                String path = "https://www.networkmms.xyz/banci/getRule" + "?eqId=" + ENV.eqId;
                                URL url = new URL(path);
                                connection = (HttpURLConnection) url.openConnection();
                                //设置请求方法
                                connection.setRequestMethod("GET");
                                //设置连接超时时间（毫秒）
                                connection.setConnectTimeout(5000);
                                //设置读取超时时间（毫秒）
                                connection.setReadTimeout(5000);

                                //返回输入流
                                InputStream in = connection.getInputStream();
                                //读取输入流
                                reader = new BufferedReader(new InputStreamReader(in));
                                StringBuilder result = new StringBuilder();
                                String line;
                                while ((line = reader.readLine()) != null) {
                                    result.append(line);
                                }

                                JSONObject jo = new JSONObject(result.toString());

                                if (!"".equals(jo.getString("OPENID_"))) {
                                    ENV.userId = jo.getString("OPENID_");
                                    ENV.eqId = jo.getString("EQID_");

                                    ENV.STIME_ = jo.getString("STIME_");
                                    ENV.ETIME_ = jo.getString("ETIME_");
                                    ENV.sTime = Utils.processTime(jo.getString("STIME_"));
                                    ENV.eTime = Utils.processTime(jo.getString("ETIME_"));
                                    ENV.delaiyTime = jo.getInt("FLEX_NUM_");
                                    ENV.status = jo.getInt("STATUS_");
                                    if(ENV.status == 1 && !ScheduleService.state){
                                        ENV.context.startService(ENV.intent);
                                    }else if(ENV.status == 0 && ScheduleService.state){
                                        if(ENV.context != null){
                                            if(ENV.intent != null) {
                                                ENV.context.stopService(ENV.intent);
                                            }
                                        }
                                    }
                                }

                            } catch (MalformedURLException e) {
                                e.printStackTrace();
                            } catch (ProtocolException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            } finally {
                                if (reader != null) {
                                    try {
                                        reader.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if (connection != null) {//关闭连接
                                    connection.disconnect();
                                }
                            }
                        }
                        try {
                            Thread.sleep(30 * 1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
        if(thread.getState().equals(Thread.State.NEW)){
            thread.start();
        }
    }

    public static void stop(){
        loop = false;
    }
}
